package the.guanzhong.express.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import the.guanzhong.express.infrastructure.mapper.ExpressBooksMapper;
import the.guanzhong.express.infrastructure.model.BooksDO;
import the.guanzhong.express.interfaces.controller.req.BooksAddReq;
import the.guanzhong.express.interfaces.controller.req.BooksListReq;
import the.guanzhong.express.interfaces.controller.req.BooksModifyReq;
import the.guanzhong.express.interfaces.controller.req.BooksRemoveReq;
import the.guanzhong.express.interfaces.controller.resp.BooksListResp;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 地址薄服务类
 */
@Service
public class ExpressBooksService {

    @Resource
    private ExpressBooksMapper expressBooksMapper;

    /**
     * 新增地址薄
     * @param req   地址薄信息
     * @return
     */
    public boolean add(BooksAddReq req) {
        BooksDO entity = new BooksDO();
        entity.setTenantId(req.getTenantId());
        entity.setUserId(req.getUserId());
        entity.setShopId(req.getShopId());
        entity.setShopName(req.getShopName());
        entity.setAddress(req.getAddress());
        entity.setLinkman(req.getLinkman());
        entity.setPhone(req.getPhone());
        entity.setCreatedTime(new Date());
        entity.setModifiedTime(new Date());
        return expressBooksMapper.insert(entity) > 0;
    }

    /**
     * 删除地址薄
     * @param req   地址薄信息
     * @return
     */
    public boolean remove(BooksRemoveReq req) {
        return expressBooksMapper.update(null, new LambdaUpdateWrapper<BooksDO>()
                .set(BooksDO::getIsDeleted, 1)
                .set(BooksDO::getModifiedTime, new Date())
                .in(BooksDO::getId, req.getBooksIdList())
        ) > 0;
    }

    /**
     * 修改地址薄
     * @param req   地址薄信息
     * @return
     */
    public boolean modify(BooksModifyReq req) {
        BooksDO entity = new BooksDO();
        entity.setId(req.getBooksId());
        entity.setShopId(req.getShopId());
        entity.setShopName(req.getShopName());
        entity.setAddress(req.getAddress());
        entity.setLinkman(req.getLinkman());
        entity.setPhone(req.getPhone());
        entity.setModifiedTime(new Date());
        return expressBooksMapper.updateById(entity) > 0;
    }

    /**
     * 查询地址簿列表
     * @param req   查询条件
     * @return
     */
    public List<BooksListResp> list(BooksListReq req) {
        List<BooksDO> booksList = expressBooksMapper.selectList(new LambdaQueryWrapper<BooksDO>()
                .eq(BooksDO::getUserId, req.getUserId())
                .orderByDesc(BooksDO::getCreatedTime)
        );
        return CollectionUtils.isEmpty(booksList) ? null : booksList.stream().map(this::convertOf).collect(Collectors.toList());
    }

    /**
     * 数据类型状态
     * @param source 原类型
     * @return
     */
    private BooksListResp convertOf(BooksDO source) {
        BooksListResp target = new BooksListResp();
        target.setBooksId(source.getId());
        target.setAddress(source.getAddress());
        target.setLinkman(source.getLinkman());
        target.setPhone(source.getPhone());
        target.setShopId(source.getShopId());
        target.setShopName(source.getShopName());
        target.setTenantId(source.getTenantId());
        return target;
    }
}
